/**
 * mssql模块简单封装
 */
var mssql = require('mssql');
var db = {};
var config = {
    user: 'sa',
    password: '123qwe!@#',
    server: '112.74.191.20',
    database: 'Ulife_ERP_TEST_NEW',
    port: 1433,
    options: {
        encrypt: false,
        connectTimeout:15000000,
        requestTimeout:15000000,
    },
    pool: {
        min: 0,
        max: 10,
        idleTimeoutMillis: 30000,
    },
};

//执行sql,返回数据.
db.sql = function(sql, callBack) {
    try {
        var connection = new mssql.Connection(config, function(err) {
            if (err) {
                console.log(err);
                return;
            }
            try {
                var ps = new mssql.PreparedStatement(connection);
                ps.prepare(sql, function(err) {
                    if (err) {
                        console.log(err);
                        return;
                    }
                    ps.execute('', function(err, result) {
                        if (err) {
                            console.log(err);
                            return;
                        }
                        ps.unprepare(function(err) {
                            if (err) {
                                console.log(err);
                                callback(err, null);
                                return;
                            }
                            callBack(err, result);
                        });
                    });
                });
            } catch (error) {}
        });
    } catch (error) {
        console.log(' connect database error : ' + error);
    }
};

module.exports = db;